/**
 * @author YinHang
 */
;(function(){
	var ROOT_PATH = "/javascript/GameFramework";
	var MODULE_PATH = "/module"
	var FRAMEWORK_NAME = "GameFramework";
	var FRAMEWORK_MAINFILENAME = "Framework.js";
	var BASE_CLASSES = {};
	var LAST_LOAD_PACKAGE = "";
	var LAST_LOAD_PACKAGE_ANOTHERNAME = "";
	var DEBUG = false;
	var IMAGES_HASHLIST = {};
	var IMAGE_COUNTER = 0;
	var CONSOLE = null;
	var CONSOLE_MESSAGE_COUNT = 0;
	var CONSOLE_MAX_MESSAGE = 48;
	
    window.GF = window.GameFramework = {
		classList: {},
		debug: function (debug) {
			DEBUG = debug;
		},
		setConsoleMaxMessage: function (max) {
			CONSOLE_MAX_MESSAGE = max;
		},
		println: function (msg) {
			if(DEBUG)
			{
				if(CONSOLE == null)
				{
					CONSOLE = document.createElement("div");
					CONSOLE.style.height = "128px";
					CONSOLE.style.fontSize = "12px";
					CONSOLE.style.backgroundColor = "#000000";
					CONSOLE.style.color = "#ffffff";
					CONSOLE.style.overflow = "scroll";
					CONSOLE.style.padding = "4px";
					CONSOLE.style.borderTop = "2px solid #cccccc";
					CONSOLE.style.borderLeft = "2px solid #cccccc";
					document.body.appendChild(CONSOLE);
				}
				if(( CONSOLE_MESSAGE_COUNT > CONSOLE_MAX_MESSAGE ) || ( ++ CONSOLE_MESSAGE_COUNT > CONSOLE_MAX_MESSAGE ))
				{
					var divs = CONSOLE.getElementsByTagName("div");
					if(divs && divs.length > 0)
						CONSOLE.removeChild(divs[0])
				}
				CONSOLE.innerHTML += "<div><span style='color: #cccccc;font-size: 10px;'>@" + new Date().getTime() + "-mm" + CONSOLE_MAX_MESSAGE + ": </span>" + msg + "</div>";
				CONSOLE.scrollTop = CONSOLE.scrollHeight;
			}
		},
		cleanConsole: function () {
			CONSOLE.innerHTML = "";
		},
		getRootPath: function () {
			return ROOT_PATH;
		},
		getName: function () {
			return FRAMEWORK_NAME;
		},
		getFileName: function () {
			return FRAMEWORK_MAINFILENAME;
		},
		getModulePath: function () {
			return MODULE_PATH;
		},
		getScript: function (src, callback) {
			var scriptTag = document.createElement("script");
			scriptTag.type = "text/javascript";
			scriptTag.src = src;
			scriptTag.onload = callback;
			document.getElementsByTagName("head")[0].appendChild(scriptTag);
		},
		saveImage: function (name, src) {
			if(!IMAGES_HASHLIST[name])
				++ IMAGE_COUNTER;
			IMAGES_HASHLIST[name] = new ui.g2d.Image(src);
		},
		loadImage: function (name, callback) {
			var image = IMAGES_HASHLIST[name];
			if(image)
				image.load(callback);
		},
		loadAllImage: function (callback) {
			var loaded = 0;
			function callbackTrigger() {
				if(++ loaded == IMAGE_COUNTER)
					if(callback)
						callback();
			};
			for(var name in IMAGES_HASHLIST)
			{
				if(IMAGES_HASHLIST[name].loaded != true)
					GameFramework.loadImage(name, callbackTrigger);
				else
				{
					callbackTrigger();
				}
			}
		},
		getImage: function (name) {
			return IMAGES_HASHLIST[name];
		},
		merge: function (obj1, obj2) {
			for(var key in obj2)
			{
				obj1[key] = obj2[key];
			}
		},
		register: function (prototype) {	//将框架文件夹下的一个类注册到框架中，一个js文件只会有一个类注册成功，类名为文件路径名,并可自定义别名。
			if(LAST_LOAD_PACKAGE_ANOTHERNAME && LAST_LOAD_PACKAGE)
			{
				GameFramework.giveName(LAST_LOAD_PACKAGE_ANOTHERNAME, prototype);
				LAST_LOAD_PACKAGE_ANOTHERNAME = null;
				GameFramework.giveName(LAST_LOAD_PACKAGE, prototype);
				LAST_LOAD_PACKAGE = null;
			}
		},
		giveName: function (name, obj) {
			var paths = name.split(".");
			var pathnum = paths.length;
			var finalObj = window[paths[0]] = window[paths[0]] ? window[paths[0]] : obj;
			for(var i=1;i<pathnum;++i)
			{
				finalObj = finalObj[paths[i]] = finalObj[paths[i]] ? finalObj[paths[i]] : obj;
			}
		},
		inherit: function (base) {
			return function (constructor) {
				var Base;
				try{
					eval("Base = " + base);
				} catch(e) {
					alert("基类不存在: " + base);
					return null;
				}
				GameFramework.merge(constructor.prototype, new Base());
				BASE_CLASSES[constructor] = Base;
				constructor.base = BASE_CLASSES[constructor];
				for(var key in constructor.prototype)
				{
					if((typeof constructor.prototype[key]).toLowerCase() == "function")
						constructor.prototype["$_"+ key] = constructor.prototype[key];
				}
				constructor.prototype.constructor = constructor;
				return constructor;
			};
		},
		load: function (options) {
			var index = 0;
			loader = function () {
				if(options.classes[index])
				{
					var np = options.classes[index].split(":");
					var className = np[0];
					var path = np[1] ? np[1] : np[0];
					var curIndex = index;
					++ index;
					LAST_LOAD_PACKAGE_ANOTHERNAME = className;
					LAST_LOAD_PACKAGE = path;
					GameFramework.getScript(ROOT_PATH + MODULE_PATH + "/" + path.split(".").join("/") + ".js", loader);
				}
				else if(options.complete)
				{
					options.complete();	
				}
			};
			loader();
		},
        setRootPath: function(path){
			if(path)
			{
				ROOT_PATH = path;
				return;
			}
			var scripts = document.getElementsByTagName("script");
			var scriptnum = scripts.length;
			for(var i=0;i<scriptnum;++i)
			{
				if(scripts[i].src) 
				{
					var paths = scripts[i].src.split("/");
					var fileName = paths[paths.length - 1];
					if(fileName == GameFramework.getFileName())
					{
						if(paths[paths.length - 2] == GameFramework.getName())
						{
							paths.splice(paths.length - 1, 1);
							ROOT_PATH = paths.join("/");
							break;
						}
					}
				}
			}
        }
    };
	
	//添加到全局的一些工具函数
	window.println = GameFramework.println;
	//自动设置根路径
	GameFramework.setRootPath();
})();
